﻿using System;
using System.Linq;
using System.Windows.Forms;
using S7.Net;
using S7PlcDataCollector.Models;
using S7PlcDataCollector.Service;
using ZbSkin.Frm;

namespace S7PlcDataCollector.Client.Forms
{
    public partial class FrmPlcVariableSetup : PopupForm
    {
        private readonly bool _addFlag;
        private readonly S7PlcVariableEntity _model;

        private static readonly string DataType = "PLC变量";

        public FrmPlcVariableSetup(int s7PlcEntityRid)
            : this(true, new S7PlcVariableEntity(), s7PlcEntityRid)
        {
        }

        public FrmPlcVariableSetup(S7PlcVariableEntity model, int s7PlcEntityRid)
            : this(false, model, s7PlcEntityRid)
        {
        }

        private FrmPlcVariableSetup(bool addFlag, S7PlcVariableEntity model, int s7PlcEntityRid)
        {
            InitializeComponent();
            _addFlag = addFlag;
            _model = model;
            _model.S7PlcEntityRid = s7PlcEntityRid;
        }

        private void FrmPlcVariableSetup_Load(object sender, EventArgs e)
        {
            var supportedPlcVariableTypeList = S7PlcDataCollectorService.Instance.SupportedPlcVariableTypeList;
            cmbVarType.DataSource = supportedPlcVariableTypeList;

            if (!_addFlag && _model != null)
            {
                textBoxVariable.Text = _model.Variable;
                cmbVarType.SelectedItem = _model.VariableTypeEnum;
            }

            textBoxVariable.Focus();
            textBoxVariable.SelectionStart = textBoxVariable.TextLength;

            FrmTitle = _addFlag ? $"添加{DataType}" : $"编辑{DataType}";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!CheckParas())
            {
                return;
            }

            var models = S7PlcDataCollectorService.Instance
                .GetS7PlcVariableEntityList(_model.S7PlcEntityRid);
            var model = models.FirstOrDefault(t => _model.Variable == t.Variable);

            if (_addFlag)
            {
                AddPlcVariable(model);
            }
            else
            {
                UpdatePlcVariable(model);
            }
        }

        private void tnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddPlcVariable(S7PlcVariableEntity model)
        {
            if (model != null)
            {
                MessageBoxEx.Warning($"{DataType}已存在", this);
                return;
            }

            if (S7PlcDataCollectorService.Instance.AddS7PlcVariableEntity(_model) > 0)
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBoxEx.Warning($"添加{DataType}失败", this);
            }
        }

        private void UpdatePlcVariable(S7PlcVariableEntity model)
        {
            if (model != null && model.Rid != _model.Rid)
            {
                MessageBoxEx.Warning($"{DataType}已存在", this);
                return;
            }

            if (S7PlcDataCollectorService.Instance.UpdateS7PlcVariableEntity(_model))
            {
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBoxEx.Warning($"修改{DataType}失败", this);
            }
        }

        private bool CheckParas()
        {
            var variable = textBoxVariable.Text.Trim();
            if (string.IsNullOrWhiteSpace(variable))
            {
                MessageBoxEx.Warning($"请输入{DataType}变量名称", this);
                return false;
            }

            _model.Variable = variable;
            _model.VariableType = (int) (VarType) cmbVarType.SelectedItem;
            return true;
        }
    }
}
